﻿using GN.Pay.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.AliPay.Request
{
    /// <summary>
    /// Wap 请求
    /// </summary>
    [Serializable]
    class WapPayRequest : AService
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="order"></param>
        /// <param name="config"></param>
        public WapPayRequest(ITradeChannel channel,
           IOrderPayApply order,
           AliPayConfigure config)
        {
            this.Init(channel, order, config);
        }

        /// <summary>
        /// 创建请求
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="order"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        private void Init(ITradeChannel channel,
            IOrderPayApply order,
            AliPayConfigure config)
        {
            if (channel == null)
            {
                ExceptionUtils.CheckNotNull(channel, nameof(channel));
            }
            if (order == null)
            {
                channel.ThrowChannelValidationException("请求的 order 为 null");
            }
            if (config == null)
            {
                channel.ThrowChannelValidationException("请求的 config 为 null");
            }
            if (config.ConfigureInfo == null)
            {
                channel.ThrowChannelValidationException("请求的 config 的 ConfigureInfo 为 null");
            }
            Aapp_id = config.ConfigureInfo.AppId;
            Return_Url = config.PayWapReturnUrl;          
            Charset = config.Encoding.WebName;
            Sign_Type = "RSA";
            Timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            Version = "1.0";
            Notify_Url = config.PayNotifyUrl;

            Seller_id = config.ConfigureInfo.AccountId;
            Out_Trade_No = order.OrderNo;
           
            //Payment_Type = "1";
          
            Subject = FixedLengthString(order.Subject, 128);
          //  Total_Fee = Math.Round(order.OrderMoney, 2);
            Body = FixedLengthString(order.Body, 1000);
           // It_b_pay = DiffDate(order);
           
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        protected string DiffDate(IOrderPayApply order)
        {
            var ts = order.ExpireTime - order.StartTime;
            if (ts.TotalMinutes > 0)
            {
                return ((int)ts.TotalMinutes).ToString() + "m";
            }
            return "";
        }

        /// <summary>
        /// 支付宝分配给开发者的应用ID
        /// </summary>
        public string Aapp_id { get; set; }

        /// <summary>
        /// 接口名称
        /// </summary>
        public string Method { get; } = "alipay.trade.wap.pay";

        /// <summary>
        /// 
        /// </summary>
        public string Format { get; } = "JSON";

        /// <summary>
        /// 返回Url
        /// </summary>
        public string Return_Url { get; set; }

        /// <summary>
        /// 请求使用的编码格式，如utf-8,gbk,gb2312等
        /// </summary>
        public string Charset { get; set; }

        /// <summary>
        /// 签名
        /// </summary>
        public string Sign_Type { get; set; }

        /// <summary>
        /// 签名
        /// </summary>
        public string Sign { get; set; }

        /// <summary>
        /// 发送请求的时间，格式"yyyy-MM-dd HH:mm:ss"
        /// </summary>
        public string Timestamp { get; set; }

        /// <summary>
        /// 调用的接口版本，固定为：1.0
        /// </summary>
        public string Version { get; set; }

        /// <summary>
        /// 支付宝服务器主动通知商户服务器里指定的页面http/https路径。
        /// </summary>
        public string Notify_Url { get; set; }

        /// <summary>
        /// 业务请求参数的集合，最大长度不限，除公共参数外所有请求参数都必须放在这个参数中传递，具体参照各产品快速接入文档
        /// </summary>
        public string Biz_Content { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string Body { get; set; }

        /// <summary>
        /// 商品的标题/交易标题/订单标题/订单关键字等。
        /// </summary>
        public string Subject { get; set; }

        /// <summary>
        /// 商户网站唯一订单号
        /// </summary>
        public string Out_Trade_No { get; set; }

        /// <summary>
        /// 该笔订单允许的最晚付款时间，逾期将关闭交易。取值
        /// </summary>
        public string Timeout_Express { get; set; }

        /// <summary>
        /// 订单总金额，单位为元，精确到小数点后两位，取值范围[0.01,100000000]
        /// </summary>
        public decimal Total_Amount { get; set; }

        /// <summary>
        /// 收款支付宝用户ID。 如果该值为空，则默认为商户签约账号对应的支付宝用户ID(2088102147948060)
        /// </summary>
        public string Seller_id { get; set; }

        /// <summary>
        /// (必须)销售产品码，商家和支付宝签约的产品码(QUICK_WAP_PAY)
        /// </summary>
        public string Product_Code { get; set; }

        /// <summary>
        /// 商品主类型：0—虚拟类商品，1—实物类商品 注：虚拟类商品不支持使用花呗渠道
        /// </summary>
        public string Goods_Type { get; set; }

        /// <summary>
        /// 公用回传参数(512)
        /// </summary>
        public string Passback_Params { get; set; }

        /// <summary>
        /// 业务扩展参数，详
        /// </summary>
        public string Extend_Params { get; set; }
    }
}
